package com.mindao.app.lxf.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mindao.app.common.spring.controller.BaseBaseController;
import com.mindao.app.lxf.model.FilePojo;


/*
 * 展示D盘文件
 * controller
 */
@Controller
@RequestMapping("/lxf")
public class ShowFileController extends BaseBaseController{
	@RequestMapping(value="/showfile",method=RequestMethod.GET)
	public String showfile(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model rhs){
		//System.out.println("测试");
		//展示文件目录页面
		List<FilePojo> list = new ArrayList<>();
		fileList("d:/",1,list);
		rhs.addAttribute("file", list);
		String rtns= super.rtnDefault(request, response, session, rhs);
		return rtns;
	}
	
	//递归，寻找三层
	public void fileList(String filePath,int flag,List list){
		File srcFile = new File(filePath);
			File[] file = srcFile.listFiles();
			if(file != null && flag <= 3){
				for(int i=0;i<file.length;i++){
					if(file[i].isDirectory()){
						flag++;
						fileList(file[i].getAbsolutePath(),flag,list);
						FilePojo filePojo = new FilePojo(file[i].getName(), file[i].getPath().substring(0,file[i].getPath().length() - file[i].getName().length()), "文件夹");
						list.add(filePojo);
					}
					else if(file[i].isFile()){
						flag = 1;
						FilePojo filePojo = new FilePojo(file[i].getName(), file[i].getPath().substring(0,file[i].getPath().length() - file[i].getName().length()), "文件");
						list.add(filePojo);
					}else{
						continue;
					}
				}
			}
		}
		
	
}
